<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div></div>
    <button>停止按钮</button>
    <script>
    //  动画的原理：通过定时器，让元素的位置不停的移动，符合条件后，自动停止。
    var div = document.querySelector('div');
    var button = document.querySelector('button');
    var timer = setInterval(() => {
        div.style.left = div.offsetLeft + 5 + 'px';
        if(div.offsetLeft > 400){
            clearInterval(timer);
        }
    },50);
    button.addEventListener('click',() =>{
        clearInterval(timer);
    });
    </script>
</body>
</html>